package com.ymatou.query.trading.api.parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ymatou.query.seller.trading.api.parameter.Common;
import com.ymatou.query.seller.trading.api.parameter.GetExportOrderInfoResult;
import com.ymatou.query.seller.trading.api.parameter.SellerExportedOrderInfo;
import com.ymatou.query.seller.trading.api.parameter.Ymt_O_OrderNote;
import com.ymatou.query.seller.trading.api.parameter.Ymt_Order;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderExtension;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderInfo;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderState;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderSummary;
import com.ymttest.common.entitie.ExportOrderInfo;
import com.ymttest.utils.DataUtil;

public class GetExportOrderInfo {
	public static List<SellerExportedOrderInfo> GetExportOrderInfos(
			GetExportOrderInfoResult result) {
		List<SellerExportedOrderInfo> exportOrderInfoList = new ArrayList<SellerExportedOrderInfo>();
		if (DataUtil.isNullOrEmpty(result.getOrders())) {
			return exportOrderInfoList;
		}
		for (Ymt_Order order : result.getOrders()) {
			SellerExportedOrderInfo export = new SellerExportedOrderInfo();
			int iOrderId = order.getiOrderId();
			List<Ymt_OrderInfo> orderInfoList = result.getOrderInfos().stream()
					.filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			List<Ymt_OrderState> orderState = result.getOrderStates().stream()
					.filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			List<Ymt_O_OrderNote> orderNote = result.getOrderNotes().stream()
					.filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			List<Ymt_OrderSummary> orderSummary = result.getOrderSummaries()
					.stream().filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			export.setOrderNo(iOrderId);
			export.setFreight(order.getfFreight());
			export.setDepositAmount(order.getfOrderPrice());
			export.setOrderState(order.getiTradingStatus());
			export.setRiskVerifiedStatus(order.getiRiskVerifiedStatus());
			export.setIsPaidInFull(order.isbPaidInFull());
			export.setAddTime(order.getdAddTime());
			export.setPayTime(order.getdPaidTime());
			export.setApplyPostPayTime(order.getdApplyPostPayTime());
			export.setPostPayTime(order.getdPostPaidTime());
			export.setDispathTime(order.getdDispathTime());
			export.setLeaveWord(order.getsLeaveWord());
			export.setBuyerLoginId(order.getsBuyerLoginId());
			export.setReceivePerson(order.getsReceivePerson());
			export.setPhone(order.getsPhone());
			export.setTelephone(order.getsTelephone());
			export.setAddress(order.getsAddress());
			export.setPostCode(order.getsPostCode());
			export.setLogisticsCompany(DataUtil.isNullOrEmpty(orderSummary) == false ? orderSummary
					.get(0).getsLogisticsProvider() : "");
			export.setLogisticsCode(DataUtil.isNullOrEmpty(orderSummary) == false ? orderSummary
					.get(0).getsSummary() : "");

			export.setOrderNote(DataUtil.isNullOrEmpty(orderNote) == false ? orderNote
					.get(0).getsContent() : "");
			export.setRemarkLevel(DataUtil.isNullOrEmpty(orderNote) == false ? orderNote
					.get(0).getiRemarkLevel() : null);
			export.setPackageDelivery_DomesticDelivered(Common
					.GetPackageDeliveryDomesticDeliveredStatus(
							DataUtil.Integerr.GetValueOrDefault(order
									.getiTradingStatus()), orderInfoList.get(0)
									.getiCatalogStatus(), order
									.getbDomesticDelivered()));
			if (DataUtil.isNullOrEmpty(orderState) == false) {
				export.setPostPayAmount(Ymt_OrderExtension
						.GetPostPaidAmount(orderState.get(0)));
				export.setCashAmount(Ymt_OrderExtension
						.GetCurrentAmountOfCash(orderState.get(0)));
				export.setTotalAmount(Ymt_OrderExtension
						.GetOrderCurrentPrice(orderState.get(0)));
				export.setRefundAmount(Ymt_OrderExtension
						.GetRefundedAmount(order,orderState.get(0)));
			}
			SetOrderInfos(export, order, orderInfoList);
			exportOrderInfoList.add(export);
		}
		return exportOrderInfoList.stream()
				.sorted((a, b) -> b.getOrderNo() - a.getOrderNo())
				.collect(Collectors.toList());
	}

	private static void SetOrderInfos(ExportOrderInfo info, Ymt_Order order,
			List<Ymt_OrderInfo> orderInfos) {
		info.setOrderType(Ymt_OrderExtension.GetOrderType(order, orderInfos));
		info.setDeliveryType(Ymt_OrderExtension.GetDeliveryMethod(orderInfos));

		if (false == DataUtil.isNullOrEmpty(orderInfos)) {
			Ymt_OrderInfo t = orderInfos.get(0);
			info.setCatalogStatus(t.getiCatalogStatus());
			info.setCatalogType(t.getiCatalogType());
			info.setBondedArea(DataUtil.Integerr.GetValueOrDefault(t
					.getiBondedArea()));
		}

		StringBuilder pname = new StringBuilder();
		StringBuilder pp = new StringBuilder();
		StringBuilder pno = new StringBuilder();
		StringBuilder pnum = new StringBuilder();
		StringBuilder ci = new StringBuilder();
		boolean delimeter = true;
		// for (Ymt_OrderInfo orderInfo : orderInfos) {
		for (int i = 0; i < orderInfos.size(); i++) {
			Ymt_OrderInfo orderInfo = orderInfos.get(i);
			GetProductNames(pname, orderInfo.getsTitle());
			GetProductPrice(pp, orderInfo.getfOriginalPrice().toString());
			GetProductNum(pnum,
					DataUtil.Integerr.GetValueOrDefault(orderInfo.getiAmount())
							.toString());
			GetProductNos(pno, orderInfo.getsSKU());
			// if (delimeter) {
			// if (ci.length() > 0) {
			// ci.append(';');
			// }
			// }
			// else {
			// delimeter = true;
			// }
			if (i == orderInfos.size() - 1) {
				delimeter = false;
			}
			GetCatalogInfos(ci, orderInfo.getsPropertyInfo(), delimeter);
		}
		info.setProductNames(pname.toString());
		info.setProductNos(pno.toString());
		info.setProductNum(pnum.toString());
		info.setProductPrice(pp.toString());
		info.setCatalogInfos(qufenhao(ci.toString()));
	}
	
	private static String qufenhao(String str) {
		while (str.length()>0&&str.substring(str.length() - 1, str.length()).equals(";")) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}

	private static void GetProductNames(StringBuilder pname, String s) {
		if (pname.length() > 0) {
			pname.append(';');
		}
		pname.append(DataUtil.Stringg.isNullOrEmpty(s) ? DataUtil.Stringg.Empty
				: s.replace("？", ""));
	}

	private static void GetProductPrice(StringBuilder pp, String s) {
		if (pp.length() > 0) {
			pp.append(';');
		}
		pp.append(DataUtil.Stringg.getValueOrDefault(s));
	}

	private static void GetProductNum(StringBuilder pno, String s) {
		if (pno.length() > 0) {
			pno.append(';');
		}
		pno.append(DataUtil.Stringg.getValueOrDefault(s));
	}

	private static void GetProductNos(StringBuilder sb, String sku) {
		if (DataUtil.Stringg.isNullOrEmpty(sku))
			return;

		for (String z : sku.split(";")) {
			if (DataUtil.Stringg.isNullOrEmpty(z))
				continue;
			if (sb.length() > 0) {
				sb.append(';');
			}
			sb.append(z.contains(":") ? z.substring(0, z.indexOf(":")) : z);
		}
	}

	private static void GetCatalogInfos(StringBuilder sb, String propertyInfo,
			boolean delimeter) {
		if (DataUtil.Stringg.isNullOrEmpty(propertyInfo))
			return;

		int n;
		StringBuilder sb2 = new StringBuilder();
		for (String z : propertyInfo.split(",")) {
			if (DataUtil.Stringg.isNullOrEmpty(z))
				continue;

			n = z.lastIndexOf(':');
			if (n + 1 == z.length())
				continue;

			if (sb2.length() > 0) {
				sb2.append(':');
			}

			String strSecondProperty = z.substring(n + 1);
			n = strSecondProperty.lastIndexOf('#');

			if (n >= 0) {
				if (n + 1 == strSecondProperty.length()) {
					sb2.append(DataUtil.Stringg.Empty);
				} else {
					sb2.append(strSecondProperty.substring(n + 1));
				}
			} else {
				sb2.append(strSecondProperty);
			}
		}
		if (sb2.length() > 0) {
			sb.append(sb2);
			if (delimeter) {
				sb.append(";");
			}
		}
	}
}
